package io.renren.midplat.fuseclick.network;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import io.renren.common.main.MailService;
import io.renren.common.utils.CharUtils;
import io.renren.common.utils.DateUtils;
import io.renren.common.utils.HttpUtils;
import io.renren.midplat.fuseclick.model.GetOffersDTO;
import io.renren.midplat.fuseclick.model.OfferCapDTO;
import io.renren.midplat.fuseclick.model.OfferGeoDTO;
import io.renren.midplat.fuseclick.model.ResultDTO;
import io.renren.modules.advertiser.entity.AdvertiserEntity;
import io.renren.modules.log.entity.PushLogEntity;
import io.renren.modules.log.service.PushLogService;
import io.renren.modules.offer.entity.OfferEntity;
import io.renren.modules.offer.entity.OfferPlatformEntity;
import io.renren.modules.offer.service.OfferService;
import io.renren.modules.ssp.entity.SspOfferEntity;
import io.renren.modules.ssp.service.SspOfferService;
import io.renren.modules.util.service.SysSwitchService;
import org.apache.http.NameValuePair;
import org.apache.http.message.BasicNameValuePair;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import java.io.UnsupportedEncodingException;
import java.util.*;

/**
 * batchGetOffersByOfferslook为spring bean的名称
 *
 * @Description /aff/v1/batches/offers
 * @Author haijun.zhang@luckincoffee.com
 * @Date 2018-08-07 9:45
 */
@Service("pullOffersByFuseClickService")
public class PullOffersByFuseClickServiceImpl {

    /**
     * 日志
     */
    private static final Logger LOGGER = LoggerFactory.getLogger(PullOffersByFuseClickServiceImpl.class);
    @Autowired
    private OfferService offerService;
    @Value("${fuseclick.apiDomain}")
    private String apiDomain;
    @Value("${fuseclick.setOfferURI}")
    private String setOfferURI;
    @Value("${fuseclick.key}")
    private String key;

    @Value("${fuseclick.payoutPCT}")
    private String payoutPCT;

    @Autowired
    private SspOfferService sspOfferService;

    private String batchNo;
    @Autowired
    private MailService mailService;
    @Autowired
    private SysSwitchService sysSwitchService;
    @Autowired
    private PushLogService pushLogService;

    public void service() {
        try {
            Map<String,String> params = new LinkedHashMap<>();
            params.put("page","1");
            params.put("limit","500");
            params.put("status","Active");
            String url = "http://agency.wondermars.com/api/v2/getOffers?key=E766DD65E4A4029CC296609FC00AE968&page=1&limit=1&status=Active&advertiser_ids=26&event_info=0&geo_info=1&device_info=1&cap_info=1&category_info=1&traffic_type_info=0";
           // String response = HttpUtils.doGet(apiDomain + setOfferURI + key, params);
            String response = HttpUtils.doGet(url,params);
            GetOffersDTO getOffersDTO = JSON.parseObject(response,GetOffersDTO.class);
            List<GetOffersDTO.DataBean> dataBeans = getOffersDTO.getData();
            for (GetOffersDTO.DataBean dataBean : dataBeans) {
                Map<String,Object> params2 = new HashMap<>();
                params2.put("ssp_id",dataBean.getId());
                if(sspOfferService.selectByMap(params2).size()<=0){
                    SspOfferEntity sspOfferEntity = new SspOfferEntity();
                    sspOfferEntity.setName(dataBean.getName());
                    sspOfferEntity.setActive(dataBean.getStatus());
                    sspOfferEntity.setSspId((dataBean.getId()));
                    sspOfferEntity.setCountries(dataBean.getGeo_countries());
                    sspOfferEntity.setAppId(dataBean.getApp_id());
                    sspOfferService.insert(sspOfferEntity);
                }
            }
            LOGGER.info(response);
        } catch (Exception e) {
            LOGGER.error("pushOffersByFuseClickService ERROR");
            LOGGER.error(e.getMessage());
            e.printStackTrace();
        }
        LOGGER.info("pushOffersByFuseClickService end");
    }
    public static void main(String [] args){
        new PullOffersByFuseClickServiceImpl().service();
    }

}

